import 'dart:io';

import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';

/// 图片选择工具类，封装了拍照和相册选择功能
class utilImagePicker {
  static final ImagePicker _picker = ImagePicker();

  /// 拍照，返回选中的图片文件，用户取消返回 null
  static Future<File?> pickImageFromCamera() async {
    try {
      final XFile? image = await _picker.pickImage(source: ImageSource.camera);
      if (image == null) return null;
      return File(image.path);
    } catch (e) {
      debugPrint('拍照出错: $e');
      return null;
    }
  }

  /// 从相册选择单张图片，返回选中的图片文件，用户取消返回 null
  static Future<File?> pickImageFromGallery() async {
    try {
      final XFile? image = await _picker.pickImage(source: ImageSource.gallery);
      if (image == null) return null;
      return File(image.path);
    } catch (e) {
      debugPrint('选择相册图片出错: $e');
      return null;
    }
  }
  // List<File> files = await utilImagePicker.pickMultiImages();
  // if (files.isNotEmpty) {
  // setState(() {
  // controllerData.imgList.value = files;
  // });
  // }
  /// 多图选择，返回图片文件列表，用户取消返回空列表
  static Future<List<File>> pickMultiImages() async {
    try {
      final List<XFile>? images = await _picker.pickMultiImage(
        imageQuality: 80, // 控制压缩质量
      );
      if (images == null) return [];
      return images.map((xfile) => File(xfile.path)).toList();
    } catch (e) {
      debugPrint('多图选择出错: $e');
      return [];
    }
  }
}
